package cn.daniellee.plugin.ril.command;

import cn.daniellee.plugin.ril.ItemLoreLimiter;
import cn.daniellee.plugin.ril.model.LimitedLore;
import net.md_5.bungee.api.ChatColor;
import net.md_5.bungee.api.chat.ClickEvent;
import net.md_5.bungee.api.chat.ComponentBuilder;
import net.md_5.bungee.api.chat.HoverEvent;
import net.md_5.bungee.api.chat.TextComponent;
import org.apache.commons.lang.StringUtils;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;

import java.util.List;
import java.util.stream.Collectors;

public class LimiterCommand implements CommandExecutor {
	@Override
	public boolean onCommand(CommandSender commandSender, Command command, String s, String[] strings) {
		if (strings.length == 0 || strings[0].equalsIgnoreCase("help")) {
			sendHelp(commandSender);
		} else if (commandSender.isOp()) {
			if ("reload".equalsIgnoreCase(strings[0])) {
				ItemLoreLimiter.getInstance().reloadConfig();
				ItemLoreLimiter.getInstance().loadConfig();
				commandSender.sendMessage((ItemLoreLimiter.getInstance().getPrefix() + ItemLoreLimiter.getInstance().getConfig().get("reload-success", "&eConfiguration reload successfully.")).replace("&", "§"));
			} else if ("list".equalsIgnoreCase(strings[0])) {
				List<String> itemLores = ItemLoreLimiter.getInstance().getLimitedLores().stream().map(LimitedLore::getItemLore).collect(Collectors.toList());
				commandSender.sendMessage((ItemLoreLimiter.getInstance().getPrefix() + ItemLoreLimiter.getInstance().getConfig().getString("message.limited-lores", "&eThe current limit of lore has: &r") + StringUtils.join(itemLores, ", ")).replace("&", "§"));
			} else if ("add".equalsIgnoreCase(strings[0]) && strings.length > 1) {
				if (ItemLoreLimiter.getInstance().getLimitLoreByLore(strings[1]) == null) {
					ItemLoreLimiter.getInstance().getConfig().set("limit-lores." + strings[1] + ".permission", "itemlorelimiter." + strings[1]);
					ItemLoreLimiter.getInstance().saveConfig();
					LimitedLore limitedLore = new LimitedLore();
					limitedLore.setItemLore(strings[1]);
					limitedLore.setPermission("itemlorelimiter." + strings[1]);
					ItemLoreLimiter.getInstance().getLimitedLores().add(limitedLore);
					commandSender.sendMessage((ItemLoreLimiter.getInstance().getPrefix() + ItemLoreLimiter.getInstance().getConfig().getString("message.add-success", "&eAdd lore limit success.")).replace("&", "§"));
					showLimitInfo(commandSender, limitedLore);
				} else commandSender.sendMessage((ItemLoreLimiter.getInstance().getPrefix() + ItemLoreLimiter.getInstance().getConfig().getString("message.already-set", "&eThe target lore is already in the limit.")).replace("&", "§"));
			} else if ("del".equalsIgnoreCase(strings[0]) && strings.length > 1) {
				LimitedLore limitedLore = ItemLoreLimiter.getInstance().getLimitLoreByLore(strings[1]);
				if (limitedLore != null) {
					ItemLoreLimiter.getInstance().getConfig().set("limit-lores." + limitedLore.getItemLore(), null);
					ItemLoreLimiter.getInstance().saveConfig();
					ItemLoreLimiter.getInstance().getLimitedLores().remove(limitedLore);
					commandSender.sendMessage((ItemLoreLimiter.getInstance().getPrefix() + ItemLoreLimiter.getInstance().getConfig().getString("message.del-success", "&eDel lore limit success.")).replace("&", "§"));
				} else commandSender.sendMessage((ItemLoreLimiter.getInstance().getPrefix() + ItemLoreLimiter.getInstance().getConfig().getString("message.not-limit", "&eThe target lore is not in the limit.")).replace("&", "§"));
			} else if ("perm".equalsIgnoreCase(strings[0]) && strings.length > 2) {
				LimitedLore limitedLore = ItemLoreLimiter.getInstance().getLimitLoreByLore(strings[1]);
				if (limitedLore != null) {
					ItemLoreLimiter.getInstance().getConfig().set("limit-lores." + limitedLore.getItemLore() + ".permission", strings[2]);
					ItemLoreLimiter.getInstance().saveConfig();
					limitedLore.setPermission(strings[2]);
					commandSender.sendMessage((ItemLoreLimiter.getInstance().getPrefix() + ItemLoreLimiter.getInstance().getConfig().getString("message.set-success", "&eThe configuration was successfully modified.")).replace("&", "§"));
					showLimitInfo(commandSender, limitedLore);
				} else commandSender.sendMessage((ItemLoreLimiter.getInstance().getPrefix() + ItemLoreLimiter.getInstance().getConfig().getString("message.not-limit", "&eThe target world is not in the limit.")).replace("&", "§"));
			} else sendHelp(commandSender);
		} else commandSender.sendMessage((ItemLoreLimiter.getInstance().getPrefix() + ItemLoreLimiter.getInstance().getConfig().getString("message.not-limit", "&eThe target world is not in the limit.")).replace("&", "§"));
		return true;
	}

	private void showLimitInfo(CommandSender commandSender, LimitedLore limitedLore) {
		List<String> showInfo = ItemLoreLimiter.getInstance().getConfig().getStringList("message.show-info");
		if (showInfo != null && !showInfo.isEmpty()) {
			showInfo.forEach(item -> commandSender.sendMessage(item.replace("{item-lore}", limitedLore.getItemLore()).replace("{permission}", limitedLore.getPermission()).replace("&", "§")));
		}
	}

	private void sendHelp(CommandSender commandSender) {
		commandSender.sendMessage(("&m&6---&m&a--------&3 : " + ItemLoreLimiter.getInstance().getConfig().getString("prompt-prefix", "&dItemLoreLimiter") + "&3 : &m&a--------&m&6---").replace("&", "§"));

		String spawnText =  ItemLoreLimiter.getInstance().getConfig().getString("help.list-help", "&eList all limited worlds.").replace("&", "§");
		TextComponent spawnHelp = new TextComponent("/ill list  " + spawnText);
		spawnHelp.setColor(ChatColor.GRAY);
		spawnHelp.setClickEvent(new ClickEvent(ClickEvent.Action.SUGGEST_COMMAND, "/ill list"));
		spawnHelp.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new ComponentBuilder(spawnText).color(ChatColor.BLUE).create()));
		commandSender.spigot().sendMessage(spawnHelp);

		String addText = ItemLoreLimiter.getInstance().getConfig().getString("help.add-help", "&eAdd lore limit.").replace("&", "§");
		TextComponent addHelp = new TextComponent("/ill add <world>  " + addText);
		addHelp.setColor(ChatColor.GRAY);
		addHelp.setClickEvent(new ClickEvent(ClickEvent.Action.SUGGEST_COMMAND, "/ill add "));
		addHelp.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new ComponentBuilder(addText).color(ChatColor.BLUE).create()));
		commandSender.spigot().sendMessage(addHelp);

		String delText = ItemLoreLimiter.getInstance().getConfig().getString("help.del-help", "&eRemove lore limit.").replace("&", "§");
		TextComponent delHelp = new TextComponent("/ill del <world>  " + delText);
		delHelp.setColor(ChatColor.GRAY);
		delHelp.setClickEvent(new ClickEvent(ClickEvent.Action.SUGGEST_COMMAND, "/ill del "));
		delHelp.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new ComponentBuilder(delText).color(ChatColor.BLUE).create()));
		commandSender.spigot().sendMessage(delHelp);

		String permText = ItemLoreLimiter.getInstance().getConfig().getString("help.perm-help", "&eSet permissions to allow target lore.").replace("&", "§");
		TextComponent permHelp = new TextComponent("/ill perm <world> <permission>  " + permText);
		permHelp.setColor(ChatColor.GRAY);
		permHelp.setClickEvent(new ClickEvent(ClickEvent.Action.SUGGEST_COMMAND, "/ill perm "));
		permHelp.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new ComponentBuilder(permText).color(ChatColor.BLUE).create()));
		commandSender.spigot().sendMessage(permHelp);

		String reloadText = ItemLoreLimiter.getInstance().getConfig().getString("help.reload-help", "Reload configuration.").replace("&", "§");
		TextComponent reloadHelp = new TextComponent("/ill reload  " + reloadText);
		reloadHelp.setColor(ChatColor.GRAY);
		reloadHelp.setClickEvent(new ClickEvent(ClickEvent.Action.SUGGEST_COMMAND, "/ill reload"));
		reloadHelp.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new ComponentBuilder(reloadText).color(ChatColor.BLUE).create()));
		commandSender.spigot().sendMessage(reloadHelp);
	}

}
